import JSPDF from 'jspdf'

function exportPDF(
  pages: HTMLCanvasElement[],
  orientation?: 'p' | 'portrait' | 'l' | 'landscape',
  unit?: 'pt' | 'px' | 'in' | 'mm' | 'cm' | 'ex' | 'em' | 'pc',
  format: [number, number] = [21.0, 29.7],
) {
  if (!pages || pages.length <= 0)
    return
  const pdf = new JSPDF(orientation, unit, format)
  pdf.addImage({
    imageData: pages.shift()!.toDataURL('image/jpeg', 1.0),
    x: 0,
    y: 0,
    width: format[0],
    height: format[1],
    format: 'JPEG',
  })
  pages.forEach((page) => {
    pdf.addPage()
    pdf.addImage({
      imageData: page.toDataURL('image/jpeg', 1.0),
      x: 0,
      y: 0,
      width: format[0],
      height: format[1],
      format: 'JPEG',
    })
  })
  pdf.save()
}

export default exportPDF
